package cn.edu.nuc.fourth.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.School;
import cn.edu.nuc.fourth.services.interfaces.FillingService;

@RequestMapping("/school")
@Controller
public class SchoolController {

	@Autowired
	private FillingService service;

	/**
	 * 跳转填报办学
	 * 
	 * @return
	 */
	@RequestMapping("/school")
	public String initadd(HttpSession session) {

		String sdate = "2016";
		Integer result = service.noAdd(sdate);
		if(result >0){
			
			return "schooluser/schoolAdd";
		}
		return "noeditSchool";
	}

	/**
	 * 执行添加
	 * 
	 * @param school
	 * @return
	 */
	@RequestMapping(value = "/schooladd", method = RequestMethod.POST)
	public  String add(@Valid @ModelAttribute("school") School school, Errors errors, Model model) {

		if (errors.hasErrors()) {
			System.out.println("校验办学填报");
			return "schooluser/schoolAdd";
		}
		Integer result = service.schoolAdd(school);
		return "总体界面";
	}

	@RequestMapping(value = "/schoollist")
	public String schoolList(Integer userid, @RequestParam(name = "current", defaultValue = "1") Integer current,
			Model model) {

		PageInfo pageInfo = service.schoolList(-1, current);
		PageInfo  pageMajor = service.majorList(-1, 1);
		model.addAttribute("page", pageInfo);
		model.addAttribute("pagemajor", pageMajor);
		return "schooluser/school";
	}

	/**
	 * school列表之后的查询
	 * 
	 * @param sid
	 * @return
	 */
	@RequestMapping("/oneschool")
	public String oneSchool(Integer sid, Model model) {

		School school = service.oneSchool(sid);
		model.addAttribute("school", school);
		if (school.getState() == 1) {

			return "schooluser/noeditSchool";
		}

		return "schooluser/oneSchool";
	}

	@RequestMapping("/schooledit")
	public String editSchool(@ModelAttribute("s") School s, Model model) {

		System.out.println("s:" + s);
		
		School school = service.schoolEdit(s);
		//System.out.println("school:" + school);
		model.addAttribute("school", school);

		return "schooluser/oneSchool";
	}

	@RequestMapping(value = "/schooldelete", method = RequestMethod.GET)
	public String deleteSchool(Integer sid) {

		Integer result = service.schoolDelete(sid);
		return "redirect:/school/schoollist";
	}

	@RequestMapping("/noedit")
	public String noeditSchool(Integer sid, Integer state, Model model) {

		School school = service.noeditSchool(sid, state);

		model.addAttribute("school", school);
		return "schooluser/noeditSchool";
	}

	@RequestMapping(value = "/ajaxschool", method = RequestMethod.POST)
	public @ResponseBody String ajaxSchool() {

		return null;
	}
	
	@RequestMapping(value="/ajax",method=RequestMethod.POST)
	public @ResponseBody PageInfo ajaxList(Integer userid,Integer current){
		
		
		PageInfo pageInfo = service.schoolList(-1, current);
		return pageInfo;
	}
}
